layui.use(['form','layer','qsform'],function(){
    var $ = layui.jquery,
        layer = parent.layer === undefined ? layui.layer : top.layer,
        form = layui.form,
        qsform = layui.qsform;

    var ue;

    form.on("submit(addArticle)",function(data){
        // 弹出loading
        var index = top.layer.msg('数据提交中，请稍候',{icon: 16,time:false,shade:0.8});
        let _id = $(".id").val();
        let _message = "文章添加成功";
        let _formdata = qsform.getDataByQsName(data);
        _formdata.content = ue.getContent();
        console.log(_formdata);
        if(_id == null || _id == ""){
            // 新增
            $.post("../../articles/", JSON.parse(_formdata), function(res){

            })
        }else{
            // 更新
            $.ajax({
                url : "../../articles/",
                type : "PUT",
                data : JSON.parse(_formdata),
                success : function (res) {

                },
                error : function (res) {
                    layer.msg("更新失败");
                    return false;
                }
            });
            _message = "文章修改成功";
        }
        setTimeout(function(){
            layer.close(index);
            layer.msg(_message);
            layer.closeAll("iframe");
            // 刷新父页面
            parent.location.reload();
        },500);
        return false;
    });

    /* setTimeout 异步处理解决 ueditor有概率不加载内容的问题 */
    setTimeout(function(){ ue = UE.getEditor('content'); }, 50);

    // 是否公开
    form.on('switch(isPublic)', function(data){
        if(data.elem.checked){
            //$("#"+data.elem.attributes["lay-filter"].value).val(1);
            $("input[qs-name='"+data.elem.attributes["lay-filter"].value+"']").val(1);
        }else{
            //$("#"+data.elem.attributes["lay-filter"].value).val(0)
            $("input[qs-name='"+data.elem.attributes["lay-filter"].value+"']").val(0);
        }
    });

    // 是否置顶
    form.on('switch(isTopping)', function(data){
        if(data.elem.checked){
            $("input[qs-name='"+data.elem.attributes["lay-filter"].value+"']").val(1);
        }else{
            $("input[qs-name='"+data.elem.attributes["lay-filter"].value+"']").val(0);
        }
    });
});